/*
 * @Descripttion:
 * @version:
 * @Author: 穆宏宇
 * @Date: 2020-08-24 14:18:18
 * @LastEditors: 穆宏宇
 * @LastEditTime: 2020-11-30 08:52:27
 */
import Vue from 'vue'
import VueRouter from 'vue-router'

// 重写push方法
const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
	return routerPush.call(this, location).catch(error => error)
}

import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

import store from '../store'
import tools from '@/tools'

// 配置文件
import config from '@/config'

import {
	oauths
} from '@/api/admin.api'

import {
	getUserInfoByToken,
	getUserByTokenGGH,
	getRole
} from '@/api/mall.api'



Vue.use(VueRouter)

import routes from './router.all'

const router = new VueRouter({
	mode: 'history',
	base: config.publicPath,
	routes
})

router.beforeEach((to, from, next) => {
	if (to.query.client_code) {
		localStorage.setItem("client_code", to.query.client_code)
	}
	// 这里需要查询用户ID
	/**
	 * 免登返回token，根据token查询用户信息
	 * 直接返回长token，解析用户信息
	 * 直接返回用户信息
	 */
	var access_token = to.query.access_token;
	if(access_token){
		localStorage.setItem('qiantai', access_token)
		// getUserInfoByToken({
		// 	account:localStorage.getItem("account")
		// }).then((res)=>{
		// 	localStorage.setItem("userId",res.id);
		// 	localStorage.setItem("user_name",res.name);
		// 	localStorage.setItem("authorities",res.authorities)
		// 	next()
		// })
	}
	if (to.query.userId) {
		localStorage.setItem("userId", to.query.userId)
	}
	if (to.query.code) {
		localStorage.clear()
		// 钢钢好登录
		// 通过code,也就是token,提供后台调用wsdl/resful 获取用户信息
		console.log("钢钢好免登code"+to.query.code)
		localStorage.setItem("gToken", to.query.code)
		getUserByTokenGGH({
			token:to.query.code
		}).then((res)=>{
			var arr = res.replace(/"/g,"").replace("[","").replace("]","").split(",")
			if(arr[2] == 'success'){
				oauths({
					name:arr[1]
				}).then((dict)=>{
					if(dict.code == 500){
						MessageBox.confirm('钢钢好免登失败！', '温馨提示', {
							showCancelButton:false,
							confirmButtonText:"确定"
						}).then(() => {
						  next()
						}).catch(() => {})
					}else{
						localStorage.setItem('qiantai', dict.token)
						localStorage.setItem('client_code', dict.client_code)
						// getUserInfoByToken({
						// 	account:localStorage.getItem("account")
						// }).then((result)=>{
						// 	localStorage.setItem("userId",result.id);
						// 	localStorage.setItem("user_name",result.name);
						// 	getRole().then((res3)=>{
						// 		localStorage.setItem("authorities",res3)
						// 		next()
						// 	})
						// })
					}
				}).catch((err)=>{
					// MessageBox.confirm('您的钢钢好账号还没有在优采平台注册!如需下单,请前往注册,你也可以拨打客服热线400-633-6466了解注册流程', '温馨提示', {
					MessageBox.confirm('您的钢钢好账号还没有在优采平台注册!如需下单,请前往注册,你也可以拨打客服热线0412-6736851  0412-6734660了解注册流程', '温馨提示', {
						showCancelButton:false,
						confirmButtonText:"确定"
					}).then(() => {
					  next()
					}).catch(() => {})
				})
			}else{
				MessageBox.confirm('钢钢好获取用户信息失败', '温馨提示!', {
					showCancelButton:false,
					confirmButtonText:"确定"
				}).then(() => {
				  next()
				}).catch(() => {})
			}
			next()
		}).catch((err)=>{
			MessageBox.confirm('钢钢好获取用户信息失败', '温馨提示!', {
				showCancelButton:false,
				confirmButtonText:"确定"
			}).then(() => {
			  next()
			}).catch(() => {})
		})
		next()
		
	}else if(to.query.account){
		oauths({
			name:to.query.account
		}).then((dict)=>{
			if(dict.code == 500){
				MessageBox.confirm('钢钢好免登失败！', '温馨提示', {
					showCancelButton:false,
					confirmButtonText:"确定"
				}).then(() => {
				  next()
				}).catch(() => {})
			}else{
				localStorage.setItem('qiantai', dict.token)
				localStorage.setItem('client_code', dict.client_code)
				// getUserInfoByToken({
				// 	account:localStorage.getItem("account")
				// }).then((res)=>{
				// 	localStorage.setItem("userId",result.id);
				// 	localStorage.setItem("user_name",result.name);
				// 	// getRole().then((res3)=>{
				// 	// 	localStorage.setItem("authorities",res3)
				// 	// 	next()
				// 	// })
				// })
			}
		}).catch((err)=>{
			// MessageBox.confirm('您的钢钢好账号还没有在优采平台注册!如需下单,请前往注册,你也可以拨打客服热线400-633-6466了解注册流程', '温馨提示', {
			MessageBox.confirm('您的钢钢好账号还没有在优采平台注册!如需下单,请前往注册,你也可以拨打客服热线0412-6736851  0412-6734660了解注册流程', '温馨提示', {
				showCancelButton:false,
				confirmButtonText:"确定"
			}).then(() => {
			  next()
			}).catch(() => {})
		})
	}
	
	if (config.whiteList.indexOf(to.name) > -1 || tools.localRead('user_token') || tools.localRead("qiantai") || to.fullPath == '/register') {
		NProgress.start()
		// if (store.state.menu.routerUpdated !== 1) {
		//   router.addRoutes(store.getters.routerList)
		// }
		let title = to.meta.title
		document.title = title
		next()
	} else { // 没登录则跳转到登录界面
		var flag = navigator.userAgent.match(
			/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
		)
		if (to.path == '/login' || to.path == '/loginMobile') {
			next()
		} else {
			if (flag) {
				// next('/loginMobile')
				next('/login')
			} else {
				next('/login')
			}
		}
	}
})

router.afterEach(() => {
	NProgress.done()
})


export default router
